import os
import re
from typing import List


class FileSearcher:
    def __init__(self, paths_list):
        normalized_paths_list = [self.normalize_path(p) for p in paths_list]
        self.compiled_patterns = self.compile_paths_list(normalized_paths_list)

    def normalize_path(self, path: str) -> str:
        """统一路径分隔符并去除多余斜杠"""
        return os.path.normpath(path.replace('\\', '/'))

    def compile_paths_list(self, patterns: List[str]) -> List[re.Pattern]:
        """将路径编译成正则表达式"""
        compiled = []
        for pattern in patterns:
            # 处理通配符转义
            regex = re.escape(pattern)
            # 将*转换为正则的.*
            regex = regex.replace(r'\*', '.*')
            # 确保匹配完整路径
            if not regex.endswith('.*'):
                regex += '$'
            compiled.append(re.compile(regex))
        return compiled

    def check_and_get_invalid_files(self, files: List[str]) -> List[str]:
        """检查文件路径是否在目录列表中，并返回不在目录中的文件"""
        violations = []
        for file_path in files:
            normalized_path = self.normalize_path(file_path)
            matched = False
            for pattern in self.compiled_patterns:
                if pattern.fullmatch(normalized_path):
                    matched = True
                    break
            if not matched:
                violations.append(file_path)
        return violations
